package activities;

import helper.GetMyScheduleHelper;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import com.facebook.Session;
import com.matchcalendar.R;

import dataFormat.CalendarTextData;

import factories.CalendarTextDataFactory;
import fragments.SetMatchScheduleFragment;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class SetMatchScheduleActivity extends FragmentActivity {
	private Button buttonPrevious, buttonNext;
	public final int SET_MATCH_TIME_RANGE_REQUEST_CODE = 0;
	private final int MATCH_RESULT_REQUEST_CODE = 1;
	private SetMatchScheduleFragment setMatchScheduleFragment = new SetMatchScheduleFragment(true, null);
	private ArrayList<CalendarTextData> matchCalDataList = new ArrayList<CalendarTextData>();
	private boolean haveAddMatchTime = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_set_match_schedule);
		findView();
		setListenerOrAdapter();
		getSupportFragmentManager().beginTransaction().
		add(R.id.linearLayoutRoot, setMatchScheduleFragment).commit();
		new PostGetMySchedule().execute(getIntent().getExtras().getString("myFBID"));
	}
	
	private void findView() {
		buttonPrevious = (Button) findViewById(R.id.buttonPrevious);
		buttonNext = (Button) findViewById(R.id.buttonNext);
	}
	
	private void setListenerOrAdapter() {
		buttonPrevious.setOnClickListener(new ButtonPreviousClick());
		buttonNext.setOnClickListener(new ButtonNextClick());
	}
	
	private class ButtonPreviousClick implements Button.OnClickListener {

		@Override
		public void onClick(View v) {
			SetMatchScheduleActivity.this.finish();
		}
	}
	
	private class ButtonNextClick implements Button.OnClickListener {

		@Override
		public void onClick(View v) {
			if (haveAddMatchTime) {
				Bundle bundle = new Bundle(getIntent().getExtras());
				Intent intent = new Intent(SetMatchScheduleActivity.this, MatchResultActivity.class);
				
				bundle.putString("myFBID", getIntent().getExtras().getString("myFBID"));
				bundle.putParcelableArrayList("matchCalDataList", matchCalDataList);
				intent.putExtras(bundle);
				startActivityForResult(intent, MATCH_RESULT_REQUEST_CODE);
			}
			else {
				new AlertDialog.Builder(SetMatchScheduleActivity.this)
				.setTitle("尚未增加活動時間")
				.setMessage("請至少增加一個活動時間範圍")
				.setPositiveButton("好", null)
				.show();
			}
		}
	}
	
	private class PostGetMySchedule extends GetMyScheduleHelper {
		private ProgressDialog proDialog;
		
		@Override
		protected void onPreExecute() {
			proDialog = ProgressDialog.show(SetMatchScheduleActivity.this, "取得行程表", "正在取得您的行程表...");
			proDialog.setCancelable(false);
		}
		
		@Override
		protected void onPostExecute(String jsonString) {
			try {
				JSONArray respJsonArr = new JSONArray(jsonString);
				
				for (int i=0; i<respJsonArr.length(); i++) {
					CalendarTextData calTextData =
							CalendarTextDataFactory.newByJsonObject(respJsonArr.getJSONObject(i));
					
					setMatchScheduleFragment.addCalendarText(calTextData, null, null, Gravity.LEFT);
				}
				proDialog.cancel();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case SET_MATCH_TIME_RANGE_REQUEST_CODE:
					Bundle fromPreviousBundle = getIntent().getExtras();
					Bundle bundle = data.getExtras();
					ArrayList<Integer> selectDaysList = bundle.getIntegerArrayList("selectDaysList");
					int startHour = bundle.getInt("startHour");
					int startMinute = bundle.getInt("startMinute");
					int overHour = bundle.getInt("overHour");
					int overMinute = bundle.getInt("overMinute");
					String activityName = fromPreviousBundle.getString("activityName");
					
					for (int day: selectDaysList) {
						CalendarTextData calTextData = new CalendarTextData(day, startHour,
								startMinute, overHour, overMinute, -1, activityName, null);
						
						setMatchScheduleFragment.addCalendarText(calTextData, null, null, Gravity.CENTER);
						matchCalDataList.add(calTextData);
					}
					haveAddMatchTime = true;
					break;
				case MATCH_RESULT_REQUEST_CODE:
					setResult(RESULT_OK);
					SetMatchScheduleActivity.this.finish();
					break;
				default:
					Session.getActiveSession().onActivityResult(
							SetMatchScheduleActivity.this, requestCode, resultCode, data);
			}
		}
	}
}
